***** All analyses performed with Stata17
***** Last synth_runner version retrieved from https://github.com/bquistorff/synth_runner 


use "C:\'yourdirectory'\Italy Province school opening.dta" 

xtset province totweek

* Some preliminary models

xtpcse newnew l.totcases_100000 l.active_cases_100000 new_tests_1000 positivityrate rt
xtpcse newnew logpop density popstud
xtpcse newnew l.totcases_100000 l.active_cases_100000 new_tests_1000 positivityrate rt logpop density popstud

* Difference in difference
	
xtdidregress (newnew) (school), group( province ) time(totweek)

xtdidregress (newnew totcases_100000 active_cases_100000 new_tests_1000 positivityrate rt ) (school), group( province ) time(totweek)

* Synth model Figure 2
synth_runner newnew totcases_100000 logpop density popstud active_cases_100000 new_tests_1000 positivityrate rt , d(school)
effect_graphs, trlinediff(-1)
pval_graphs
	
*to compute manually the pre and post RMSPE save the following matrix in excel
matrix list e(treat_control)
	
	
	
use "C:\'yourdirectory'\Bolzano and provinces.dta" 

xtset numprovince totweek

synth_runner newnew totcases_100000 logpop density popstud active_cases_100000 new_tests_1000 positivityrate rt  , trunit(24) trperiod(37) gen_vars aggfile_v(v) aggfile_w(w)

ereturn list

single_treatment_graphs, trlinediff(1) effects_ylabels(-400(200)800) effects_ymax(800) effects_ymin(-400)

effect_graphs, trlinediff(1)

pval_graphs


**Npsynth

npsynth newnew totcases_100000 logpop density popstud active_cases_100000 new_tests_1000 positivityrate rt, panel_var(numprovince) time_var(totweek) trunit(24) trperiod(37) bandw(0.4) kern(normal) npscv n_grid(1 20)

**optimal bandwith=0.1

npsynth newnew totcases_100000 logpop density popstud active_cases_100000 new_tests_1000 positivityrate rt, panel_var(numprovince) time_var(totweek) trunit(24) trperiod(37) bandw(0.1) kern(normal) npscv n_grid(1 20) gr1 gr2 gr3 save_gr1(gr1) save_gr2(gr2) save_gr3(gr3) gr_y_name("New weekly cases (per 100000)") gr_tick(5) save_res(NPSYNTH_data)

